<?php
/*
 * Author:YangBo Created on 2009-1-22:����06:32:34
 * 
 * TODO:TODO
 * as9
 * GradeService
 * 
 */
 require_once("./BaseService.php");
 require_once("./interfaces/IGradeService.php");
 require_once("./dao/Grade.php");
 require_once("./util/ServiceUtil.php");
 class GradeService extends BaseService implements IGradeService
 {
 	protected $grade;
 	public function __construct()
 	{
 		$this->initializeDB();
 		$this->grade = new Grade();	
 	}
 	/**
 	 * 创建评分
	 * @param $target [pno,scorer,score]
	 * @return:success
	*/
 	public function create($target)
 	{
 		$pno = $target['pno'];
 		$scorer = $target['scorer'];
 		$score = $target['score'];
 		
// 		try
// 		{
// 			$this->sql = "SELECT SUM(grade.score) AS total FROM grade WHERE pno='$pno'";
// 			$this->result = mysql_query($this->sql,$this->_db);
// 			$totalNums = mysql_num_rows($this->result);
// 		}catch(Exception $exc)
// 		{
// 			return $exc;
// 		}
// 		
// 		while($row = mysql_fetch_object($this->result))
// 		{
// 			$score = ceil(($row->total+$tempScore)/($totalNums+1));
// 		}
 		try
 		{
 			$this->sql = "SELECT grade.pno,grade.scorer FROM grade WHERE grade.pno='$pno' AND grade.scorer='$scorer'";
 			$this->result = mysql_query($this->sql,$this->_db);
 			$numRows = mysql_num_rows($this->result);
 			if($numRows>=1)
 			{
 				return ServiceUtil::getFailureResult("failure","User have scored!!");
 			}else
 			{
 				try
		 		{
		 			$this->sql = "INSERT INTO grade(pno,scorer,score) VALUES('$pno','$scorer','$score')";
		 			$this->result = mysql_query($this->sql,$this->_db);
		 			try
		 			{
		 				$this->sql = "SELECT SUM(grade.score)/COUNT(*) AS avg_score FROM grade WHERE pno='$pno'";
		 				$this->result = mysql_query($this->sql,$this->_db);
		 				$row = mysql_fetch_object($this->result);
		 				$avg_score = $row->avg_score;
		 				try
		 				{
		 					$this->sql = "UPDATE photos SET photos.avg_score='$score' WHERE photos.pno='$pno'";
		 					$this->result = mysql_query($this->sql,$this->_db);
		 					return ServiceUtil::getSuccessResult("success","User score success!");
		 				}catch(Exception $exc)
		 				{
		 					return $exc;
		 				}
		 			}catch(Exception $exc)
		 			{
		 				return $exc;
		 			}
		 		}catch(Exception $exc)
		 		{
		 			return $exc;
		 		}
 			}
 		}catch(Exception $exc)
 		{
 			return $exc;
 		}
 	}
 	/**
	 * 读取评分
	 * @param $targetID pno
	 * @return:$this->grade
	*/
 	public function read($targetID)
 	{
 		if($targetID)
 		{
 			$pno = strtolower(trim($targetID));
	 		try
	 		{
	 			$this->sql = "SELECT grade.id,grade.pno,grade.scorer,grade.score,grade.scored_time FROM grade WHERE pno='$pno'";
	 			$this->result = mysql_query($this->sql,$this->_db);
	 			
	 			while($row = mysql_fetch_object($this->result))
	 			{
	 				$this->grade->id = $row->id;
	 				$this->grade->pno = $row->pno;
	 				$this->grade->scorer = $row->scorer;
	 				$this->grade->score = $row->score;
	 				$this->grade->scored_time = $row->scored_time;
	 				
	 				$this->ret[] = $this->grade;
	 			}
	 			return $this->ret;
	 		}catch(Exception $exc)
	 		{
	 			return $exc;
	 		}
 		}else
 		{
 			try
	 		{
	 			$this->sql = "SELECT grade.id,grade.pno,grade.scorer,grade.score,grade.scored_time FROM grade";
	 			$this->result = mysql_query($this->sql,$this->_db);
	 			$numRows = mysql_num_rows($this->result);
	 			for($i=0;$i<$numRows;$i++)
	 			{
	 				$row = mysql_fetch_object($this->result);
	 				
	 				$this->grade = new Grade();
	 				$this->grade->id = $row->id;
	 				$this->grade->pno = $row->pno;
	 				$this->grade->scorer = $row->scorer;
	 				$this->grade->score = $row->score;
	 				$this->grade->scored_time = $row->scored_time;
	 				
	 				$this->ret[] = $this->grade;
	 			}
	 			return $this->ret;
	 		}catch(Exception $exc)
	 		{
	 			return $exc;
	 		}
 		}
 		
 	}
 	/**
	 * 更新评分
	 * @param $target [pno,scorer,score]
	 * @return:success
	*/
 	public function update($target)
 	{
 		$pno = strtolower(trim($target[0]));
 		$scorer = strtolower(trim($target[1]));
 		$score = strtolower(trim($target[2]));
 		
 		try
 		{
 			$this->sql = "UPDATE grade SET scorer='$scorer',score='$score' WHERE pno='$pno'";
 			$this->result = mysql_query($this->sql,$this->_db);
 			return ServiceUtil::getSuccessResult("success","update score success!");
 		}catch(Exception $exc)
 		{
 			return $exc;
 		}
 	}
 	/**
	 * 删除评分
	 * @param $targetID pno
	 * @return:success
	*/
 	public function delete($targetID)
 	{
 		$pno = strtolower(trim($targetID));
 		try
 		{
 			$this->sql = "DELETE FROM grade WHERE pno='$pno'";
 			$this->result = mysql_query($this->sql,$this->_db);
 			return ServiceUtil::getSuccessResult("success","delete score success!");
 		}catch(Exception $exc)
 		{
 			return $exc;
 		}
 	}
 	
 	public function __destruct()
 	{
 		
 	}
 }
 
?>
